var totalPage;
var totalCnt;
var chargingStationData = [];
var chargingProcessData = [];
var chargingProcessList = [];
var chargingProcessDatas = [];
var url = undefined;

$(document).ready(function() {
    showLoading();
    $('title', parent.document).html($('title').text());
    selectDate("startDate");
	selectDate("endDate");
    setBaseUrl();
    $ajaxList('chargingStation', getChargingStation);
    createTableHead("chargingProcessThead", chargingProcessTable, search);
    scrollTableSuspend("scrollBody", "chargingProcessTable", "chargingProcessTable1", "chargingProcessThead", "scrollHeight");
});
function getChargingStation(data) {
	var length = data.length;
	chargingStationData = data;
	search();
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
		$("#ChargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
}
//function clickDate() {
//	if (!$("#startChargingDate").val()) {
//		$("#startChargingDate").val(date2String(getSystemTime()));
//	}
//	if (!$("#endChargingDate").val()) {
//		$("#endChargingDate").val(date2String(stringToDate($("#startChargingDate").val())));
//	}
//	search();
//}
function changeDate() {
	if (!$("#endChargingDate").val()) {
		$("#endChargingDate").val(date2String(stringToDate($("#startChargingDate").val())));
	}
	search();
}
function search(){
	var startChargingDate = $("#startChargingDate").val();
	var endChargingDate = $("#endChargingDate").val();
	if (stringToDate(startChargingDate) > stringToDate(endChargingDate)){
		showMessge("开始时间不能大于结束时间！");
	} else {
		$ajax('searchChargingProcess', 'chargingProcessSearch', searchChargingProcess);
	}
}
function searchChargingProcess(data) {
	clearTable("chargingProcessTbody");
	chargingProcessDatas = [];
	chargingProcessList = data.chargingProcessList || [];
	var length = chargingProcessList.length;
	totalPage = data && data.totalPage || 0;
	totalCnt = data && data.totalCnt || 0;
	var chargingStationName = undefined;
	for(var i=0; i<length; i++){
		chargingStationName = undefined;
		var chargingProcessFileList = chargingProcessList[i].chargingProcessFileList || [];
		var listLength = chargingProcessFileList && chargingProcessFileList.length || 0;
		for(var j=0; j<listLength; j++){
			var chargingStationID =  chargingProcessList[i].chargingStationID;
			var chargingStationLength = chargingStationData.length;
			if (!chargingStationName) {
				for(var k=0; k<chargingStationLength; k++){
					if(chargingStationID == chargingStationData[k].chargingStationID){
						chargingStationName = chargingStationData[k].chargingStationName;
						break;
					}
				}
			}
			var chargingProcessFileName = chargingProcessFileList[j] && chargingProcessFileList[j].chargingProcessFileName || "文件名错误";
			var equipmentID, chargingDate;
			var fileLength = chargingProcessFileName.length;
			if (fileLength > 18) {
				equipmentID = chargingProcessFileName.substr(0, fileLength - 18).replace(",", "");
				chargingDate = chargingProcessFileName.substr(fileLength - 18, 4) + '-' + chargingProcessFileName.substr(fileLength - 14, 2) + '-' +
				chargingProcessFileName.substr(fileLength - 12, 2) + ' ' + chargingProcessFileName.substr(fileLength - 10, 2) + ':' + 
				chargingProcessFileName.substr(fileLength - 8, 2) + ':' + chargingProcessFileName.substr(fileLength - 6, 2);
			}
			if (fileLength > 23) {
				var chargingProcessData = {
						"chargingStationID": chargingStationID, 
						"chargingStationName": chargingStationName, 
						"equipmentID": equipmentID, 
						"chargingDate": chargingDate, 
						"chargingProcessFileName": chargingProcessFileName,
						"chargingProcessFileSize": '<img src="static/images/fileBms.png" style="width:28px;height:28px;" />' + 
						formatFileSize(chargingProcessFileList[j] && chargingProcessFileList[j].chargingProcessFileSize),
						"creatDate": chargingProcessFileList[j] && chargingProcessFileList[j].creatDate
				};
			} else {
				var chargingProcessData = {
						"chargingStationID": chargingStationID, 
						"chargingStationName": chargingStationName, 
						"equipmentID": equipmentID, 
						"chargingDate": chargingDate, 
						"chargingProcessFileName": chargingProcessFileName,
						"chargingProcessFileSize": '<img src="static/images/fileCcs.png" style="width:28px;height:28px;" />' + 
						formatFileSize(chargingProcessFileList[j] && chargingProcessFileList[j].chargingProcessFileSize),
						"creatDate": chargingProcessFileList[j] && chargingProcessFileList[j].creatDate
				};
			}
			chargingProcessDatas.push(chargingProcessData);
		}
	}
	if (length == 0) {
		$("#currentPage").val("1");
	}
	createTableBody("chargingProcessTbody", chargingProcessTable, chargingProcessDatas);
	setTimeout(hideLoading, 500);
}
function imports() {
	$("#uploadFile").empty();
	$("#uploadFile").append(
			'<div class="form-group">' + 
	        '<label for="importFile" class="col-sm-3 control-label">充电过程文件</label>' +
	        '<div class="col-sm-8 partCondition"><input id="importFile" name="importFile" type="file" multiple class="form-control"/></div>' +
	        '</div>'
	);
	var option = {
		uploadUrl: "importChargingProcess",
		allowedFileExtensions : ['zip'],
		maxFileCount : 100,
		showUpload: false,
		showErrorPreview: false,
		fileActionSettings: {showUpload: false, showRemove: false, indicatorNew:"", showZoom: false, showDrag: false},
		layoutTemplates: {
			size:' <samp>({sizeText})</samp>' +
				'<button type="button" class="kv-file-remove btn btn-xs btn-default" ' + 
				'title="删除文件"><li class="glyphicon glyphicon-trash text-danger"></li></button>\n', 
			footer:  '<div class="file-thumbnail-footer">\n' +
	        	'    <div class="file-footer-caption" style="width:360px", title="{caption}">{caption}{size}</div>\n' +
	        	'    {progress} {actions}\n' +
	        	'</div>'
	    },
		footer: {}
	};
	selectFile("importFile", option);
	$("#fileinputModal").modal("show");
}
function confirm() {
	var importFile = $("#importFile").val();
	var chargingStationID = $("#ChargingStationID").val();
	if (!chargingStationID) {
		$("#ChargingStationID").tips({
			side : 2,
			msg : '请选择充电站名称',
			bg : '#AE81FF',
			time : 3
		});
		$("#ChargingStationID").focus();
	} else if (!importFile) {
		$("#importFile").tips({
			side : 2,
			msg : '请选择要上传的充电过程文件',
			bg : '#AE81FF',
			time : 3
		});
		$("#importFile").focus();
	} else {
		showLoading();
		$ajaxSubmit('importFileForm', 'importChargingProcess', importFileResult, {subfolder: chargingStationID});
	}
}
function importFileResult(data) {
	setTimeout(hideLoading, 500);
	$("#fileinputModal").modal("hide");
	var result = data && data.result;
	if (result == "ChargingStationIDNotFound") {
		showMessge("上传失败，充电站ID为空！");
	} else {
		var resultList = data && data.resultList;
		var chargingProcessFileList = data && data.chargingProcessFileList;
		var message = "充电过程文件上传完成";
		var length = resultList.length;
		for (var i=0; i<length; i++) {
			if (resultList[i] == "Success") {
				message = message + "<br>文件【" + chargingProcessFileList[i] + "】：上传成功！"
			} else if (resultList[i] == "Error") {
				message = message + "<br>文件【" + chargingProcessFileList[i] + "】：上传失败，发生异常！"
			} else if (resultList[i] == "NameError") {
				message = message + "<br>文件【" + chargingProcessFileList[i] + "】：上传失败，文件名称错误！"
			} else if (resultList[i] == "NotFound") {
				message = message + "<br>文件【" + chargingProcessFileList[i] + "】：上传失败，上传文件不存在！"
			} else if (resultList[i] == "Exists") {
				message = message + "<br>文件【" + chargingProcessFileList[i] + "】：上传失败，上传文件已存在！"
			}
		}
		$('#myMessagesModal').modal('show');
		$('#message').html(message);
		search();
	}
}
function exportFile(index){
	showMessge("确定下载吗?", "model2");
	url = 'exportChargingProcess?subfolder=' + (chargingProcessDatas[index] && chargingProcessDatas[index].chargingStationID) +
	'&&fileName=' + (chargingProcessDatas[index] && chargingProcessDatas[index].chargingProcessFileName) + '&&checkFlag=';
}
function sure(){
	showMessge("下载中......");
	$ajax(url, undefined, exportFileResult);
}
function exportFileResult(data){
	var result = data && data.result;
	if (result == "Success") {
		downloadFile(url + "OK");
		showMessge("下载成功！");
	} else if (result == "NotFound") {
		showMessge("下载失败，下载文件不存在！");
	} else {
		showMessge("下载失败，请重新下载！");
	}
}
